﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CCApLMInterpolateLocationOrientationApp
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CCApLMInterpolateLocationOrientationApp : CCApLMLocationsApp
{
  public void InterpolateLocationOrientation(
    TxObjectList locations,
    TxTransformation firstAbsLoc,
    TxTransformation secondAbsLoc,
    TxFixedAxis fixedAxis)
  {
    if (fixedAxis != TxFixedAxis.NoFixedAxis)
      this.InterpolateLocationOrientationWithFixedAxis(locations, firstAbsLoc, secondAbsLoc, fixedAxis);
    else
      this.InterpolateLocationOrientationWithNoFixedAxis(locations, firstAbsLoc, secondAbsLoc);
  }

  public void InterpolateLocationOrientationWithFixedAxis(
    TxObjectList locations,
    TxTransformation firstAbsLoc,
    TxTransformation secondAbsLoc,
    TxFixedAxis fixedAxis)
  {
    new TxLocationManipulationEx().InterpolateLocationsWithFixedAxis(locations, firstAbsLoc, secondAbsLoc, (TxAxis) fixedAxis);
  }

  public void InterpolateLocationOrientationWithNoFixedAxis(
    TxObjectList locations,
    TxTransformation firstAbsLoc,
    TxTransformation secondAbsLoc)
  {
    int num = 0;
    double dr = 0.0;
    double dp = 0.0;
    double dw = 0.0;
    this.CalculateOrientationDeltas(locations, firstAbsLoc, secondAbsLoc, ref dr, ref dp, ref dw);
    TxVector txVector = this.To_0_2PI(firstAbsLoc.RotationRPY_XYZ);
    double x = txVector.X;
    double y = txVector.Y;
    double z = txVector.Z;
    foreach (ITxObject location in (Collection<ITxObject>) locations)
    {
      if (this.InterpolateLocOrientation(location as ITxLocatableObject, x, y, z))
        ++num;
      x += dr;
      y += dp;
      z += dw;
    }
    if (num <= 0)
      return;
    TxApplication.RefreshDisplay();
  }

  private bool InterpolateLocOrientation(
    ITxLocatableObject objToChange,
    double r,
    double p,
    double w)
  {
    bool flag = false;
    TxTransformation absoluteLocation = objToChange.AbsoluteLocation;
    TxVector rotationRpyXyz = absoluteLocation.RotationRPY_XYZ;
    TxVector vec_Rad = new TxVector(r, p, w);
    if (TxVector.op_Inequality(this.To_0_2PI(rotationRpyXyz), this.To_0_2PI(vec_Rad)))
    {
      absoluteLocation.RotationRPY_XYZ = this.To_MinusPI_PI(vec_Rad);
      objToChange.AbsoluteLocation = absoluteLocation;
      flag = true;
    }
    return flag;
  }

  private void CalculateOrientationDeltas(
    TxObjectList locations,
    TxTransformation firstAbsLoc,
    TxTransformation secondAbsLoc,
    ref double dr,
    ref double dp,
    ref double dw)
  {
    int num = ((Collection<ITxObject>) locations).Count - 1;
    TxVector txVector1 = this.To_0_2PI(firstAbsLoc.RotationRPY_XYZ);
    TxVector txVector2 = this.To_0_2PI(secondAbsLoc.RotationRPY_XYZ);
    dr = txVector2.X - txVector1.X;
    if (Math.Abs(dr) > Math.PI)
    {
      if (dr > 0.0)
        dr -= 2.0 * Math.PI;
      else
        dr += 2.0 * Math.PI;
    }
    dr /= (double) num;
    dp = txVector2.Y - txVector1.Y;
    if (Math.Abs(dp) > Math.PI)
    {
      if (dp > 0.0)
        dp -= 2.0 * Math.PI;
      else
        dp += 2.0 * Math.PI;
    }
    dp /= (double) num;
    dw = txVector2.Z - txVector1.Z;
    if (Math.Abs(dw) > Math.PI)
    {
      if (dw > 0.0)
        dw -= 2.0 * Math.PI;
      else
        dw += 2.0 * Math.PI;
    }
    dw /= (double) num;
  }

  private TxVector To_0_2PI(TxVector vec_Rad)
  {
    return new TxVector(this.To_0_2PI(vec_Rad.X), this.To_0_2PI(vec_Rad.Y), this.To_0_2PI(vec_Rad.Z));
  }

  private TxVector To_MinusPI_PI(TxVector vec_Rad)
  {
    return new TxVector(this.To_MinusPI_PI(vec_Rad.X), this.To_MinusPI_PI(vec_Rad.Y), this.To_MinusPI_PI(vec_Rad.Z));
  }

  private double To_0_2PI(double v_Rad)
  {
    if (Math.Abs(v_Rad) > 2.0 * Math.PI)
      v_Rad = (double) Math.Sign(v_Rad) * (Math.Abs(v_Rad) % (2.0 * Math.PI));
    if (v_Rad < 0.0)
      v_Rad += 2.0 * Math.PI;
    return v_Rad;
  }

  private double To_MinusPI_PI(double v_Rad)
  {
    v_Rad = this.To_0_2PI(v_Rad);
    if (v_Rad > Math.PI)
      v_Rad -= 2.0 * Math.PI;
    return v_Rad;
  }

  public bool WeldSupport => TxApplication.PlatformType != 3 && TxApplication.PlatformMode != 2;
}
